<template>
  <div class="wrapper">
    <nut-infiniteloading
      @loadmore="onInfinite"
      :is-show-mod="true"
      :has-more="isHasMore"
      :is-loading="isLoading"
      :threshold="200"
    >
      <ul class="list">
        <li class="list-item" v-for="(item, index) of listData" :key="index">
          <div class="taskInfo">
            <div>任务编码：{{item.TaskCode}}</div>
            <div>任务流水号：{{item.TaskFlowNum}}</div>
            <div>受理时刻：{{item.FirstAcceptTime}}</div>
            <div>事件类型：{{item.EventType}}</div>
            <div>事件名称：{{item.EventName}}</div>
            <div>患者姓名：{{item.Name}}</div>
            <div>司机：{{item.Driver}}&nbsp;&nbsp;&nbsp;医生：{{item.Doctor}}&nbsp;&nbsp;&nbsp;护士：{{item.Nurse}}</div>
            <div v-show="item.AddCount>0" class="add-wrapper">
              <span>已录入</span>
              <span>录入次数：{{item.AddCount}}</span>
            </div>
            <div class="btnOpenWin">
              <nut-button
                type="actived"
                shape="circle"
                icon="search"
                color="rgb(0,0,0)"
                small
                @click="openPatientWin(item.TaskCode)"
              >查看</nut-button>
              <nut-button
                type="actived"
                shape="circle"
                icon="plus"
                color="rgb(0,0,0)"
                small
                @click="openWin(item.TaskCode)"
              >录入</nut-button>
            </div>
          </div>
        </li>
      </ul>
    </nut-infiniteloading>
    <nut-backtop :distance="100"></nut-backtop>
    <nut-popup
      position="bottom"
      closeable
      close-icon="cross"
      v-model="show"
      lock-scroll
      :style="{ height: '40%' }"
      close-icon-position="top-right"
      @close="closeWin"
    >
      <h1 class="title">数据录入</h1>
      <patient-form ref="patientInfo"></patient-form>
      <nut-button block shape="circle" @click="savePatientInfo">保存</nut-button>
    </nut-popup>
  </div>
</template>
<script>
import PatientForm from "./PatientForm";
export default {
  name: "TaskList",
  // props: {
  //   taskList: Array
  // },
  components: {
    PatientForm
  },
  data() {
    return {
      page: 1,
      num: 4,
      isHasMore: true,
      isLoading: false,
      isErr: false,
      timer: null,
      show: false,
      patientData: {},
      TaskCode: ""
    };
  },
  methods: {
    onInfinite() {
      this.isLoading = true;
      this.timer = setTimeout(() => {
        if (this.page <= this.maxPages) {
          this.page = this.page + 1;
        } else {
          this.isHasMore = false;
        }
        this.isLoading = false;
      }, 100);
    },
    openWin(TaskCode) {
      this.show = true;
      this.TaskCode = TaskCode;
    },
    closeWin() {
      this.$refs.patientInfo.clearData();
    },
    savePatientInfo() {
      this.$axios({
        url: "/api/Patient/SavePatientInfo",
        method: "post",
        params: {
          taskCode: this.TaskCode,
          workCode: this.$store.state.userInfo.workCode,
          patientCount: this.$refs.patientInfo.patientCount,
          diseaseLevel: this.$refs.patientInfo.diseaseLevel,
          toHospital: this.$refs.patientInfo.toHospital
        }
      }).then(res => {
        var obj = JSON.parse(res.data);
        if (obj.ret) {
          this.listData.filter(
            q => q.TaskCode == this.TaskCode
          )[0].AddCount += 1;
          this.$toast.success("保存成功");
          this.$parent.GetPatientList("", "");
        } else {
          this.$toast.fail(obj.res);
        }
      });
      this.show = false;
    },
    openPatientWin(TaskCode) {
      this.$parent.openPatientWin(TaskCode);
    }
  },
  destroyed() {
    clearTimeout(this.timer);
  },
  computed: {
    listData() {
      let list = this.$store.state.taskList;
      //console.log("list",list);
      if (list != "") {
        return list.slice(0, this.num * this.page);
      }
    },
    maxPages() {
      let list = this.$store.state.taskList;
      if (list != "") {
        return parseInt(list.length / this.num) + 1;
      }
    }
  }
};
</script>

<style scoped>
.list {
}
.list-item {
}
.taskInfo {
  height: 200px;
  background: #eee;
  margin: 10px 0;
  padding: 10px;
}
.taskInfo > div {
  margin: 5px 0;
}
.add-wrapper {
  font-weight: bold;
}
.btnOpenWin {
  text-align: right;
}
.title {
  font-weight: bold;
  margin-top: 12px;
  margin-left: 5px;
  font-size: 18px;
}
</style>
